package com.songshuai.pangu.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.songshuai.pangu.api.constant.CacheKeys;
import com.songshuai.pangu.api.dto.pangu.GetInviteServiceItemTreeResDto;
import com.songshuai.pangu.api.service.ServiceItemService;
import com.songshuai.webapi.baseapi.dto.pangu.request.service.AddInviteServiceItemReqDto;
import com.songshuai.webapi.baseapi.dto.pangu.request.service.ServiceReqDto;
import com.songshuai.webapi.baseapi.dto.pangu.response.service.InviteServiceItemTreeResDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.common.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务标准化流程
 * @Author: lhx
 * @Date: 2019/4/9 19:16
 */
@Slf4j
@RestController
@RequestMapping("/api/service/")
public class ServiceController {



    @Autowired
    private ServiceItemService serviceItemService;


    @Autowired
    private RedisUtil redisUtil;



    /**
     * 查询服务标准化列表
     * @return
     */
    @GetMapping("getAllServiceList")
    public QueryResponse<List<InviteServiceItemTreeResDto>> getAllServiceList() {
        try {
            try {
                //有缓存直接走缓存
                String result = redisUtil.getString(CacheKeys.SERVICE_ITEM_KEY);
                if (StringUtils.isNotBlank(result)) {
                    return QueryResponse.setResData(JSON.parseObject(result, new TypeReference<List<InviteServiceItemTreeResDto>>() {}));
                }
            } catch (Exception e) {
                log.error("查询服务标准化列表-redis获取转换异常", e);
            }

            List<InviteServiceItemTreeResDto> allServiceList = serviceItemService.getAllServiceList();
            if (null != allServiceList) {
                redisUtil.set(CacheKeys.SERVICE_ITEM_KEY, JSON.toJSON(allServiceList), CacheKeys.TIME_OUT_HALF_HOUR);
                return QueryResponse.setResData(allServiceList);
            }
        } catch (Exception ex) {
            log.error("/api/service/getAllServiceList", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(), ex.getMessage());
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);
    }


    /**
     * 获取邀约服务已反馈信息
     * @param reqDto
     * @return
     */
    @PostMapping("getInviteServiceItems")
    public QueryResponse<List<String>> getInviteServiceItems(@RequestBody ServiceReqDto reqDto){

        try {
            List<String> items = serviceItemService.getInviteServiceItems(reqDto);
            if (null != items){
                return QueryResponse.setResData(items);
            }
        } catch (Exception e) {
            log.error("/api/service/getInviteServiceItems", e);
            return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
        }
        return new QueryResponse<>(ResultStatus.ERROR_SEARCH_FAILED);

    }

    /**
     * 新增邀约服务反馈信息
     * @param reqDto
     * @return
     */
    @PostMapping("addInviteServiceItem")
    public QueryResponse<Boolean> addInviteServiceItem(@RequestBody AddInviteServiceItemReqDto reqDto){

        try {
            return QueryResponse.setResData(serviceItemService.addInviteServiceItem(reqDto));
        } catch (Exception ex) {
            log.error("/api/service/addInviteServiceItem", ex);
            return new QueryResponse<>(ResultStatus.FAILOfficial.getErrorCode(),ex.getMessage());
        }

    }





}
